/*
 * Copyright 2017-2025 Lenses.io Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.lenses.streamreactor.connect.io.text

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

import java.io.ByteArrayInputStream
import java.io.InputStream

class RegexMatchLineReaderTest extends AnyFunSuite with Matchers {
  test("empty input stream returns None") {
    val reader = new RegexMatchLineReader(createInputStream(""), ".*")
    reader.next() should be(None)
  }
  test("match all the lines") {
    val reader = new RegexMatchLineReader(createInputStream("value1\nvalue2\nvalue3"), ".*")
    reader.next() should be(Some("value1"))
    reader.next() should be(Some("value2"))
    reader.next() should be(Some("value3"))
    reader.next() should be(None)
  }
  test("match in the input returns the line") {
    val reader = new RegexMatchLineReader(createInputStream("1value1\nvalue2\nvalue3"), "1.*")
    reader.next() should be(Some("1value1"))
    reader.next() should be(None)
  }
  test("lines start matches a regex pattern of any digits 0-9") {
    val reader = new RegexMatchLineReader(createInputStream("1value1\nvalue2\nvalue3"), "[0-9].*")
    reader.next() should be(Some("1value1"))
    reader.next() should be(None)
    reader.next() should be(None)
  }
  test("multiple lines returned") {
    val reader = new RegexMatchLineReader(createInputStream("1value1\n2value2\n3value3"), "[0-9].*")
    reader.next() shouldBe Some("1value1")
    reader.next() shouldBe Some("2value2")
    reader.next() shouldBe Some("3value3")
    reader.next() shouldBe None
  }
  test("return files starting with 3") {
    val reader = new RegexMatchLineReader(
      createInputStream(
        """
          |1 001000001
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |2 0008    20NOV2210JAN2302NOV22
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |3 UU 002 2000003
          |3 BB 78P 02000004
          |3 CC 4  S G  YV  6042 00057677
          |5 DD 02NOV22                                                                                                                                                                               057677E057678
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
          |""".stripMargin,
      ),
      "3.*",
    )
    reader.next() shouldBe Some("3 UU 002 2000003")
    reader.next() shouldBe Some("3 BB 78P 02000004")
    reader.next() shouldBe Some("3 CC 4  S G  YV  6042 00057677")
    reader.next() shouldBe None
  }
  private def createInputStream(str: String): InputStream = new ByteArrayInputStream(str.getBytes)
}
